/*
 * GraphAdjList.c
 *
 *  Created on: 11/11/2011
 *      Author: marcio
 */

#include "Graph.h"
#include "../List.h"

Graph* graph_newGraph(int n){
	int* value = malloc(sizeof(int)*n);
	int i;
	while(i < 0)value[i] = i++;
	Graph* g = malloc(sizeof(Graph));
	g->edges = calloc(sizeof(List*)*n);
	for(i = 0; i < n; i++){
		g->edges[i] = list_newList();
	}
	g->m = 0;
	g->n = n;
	g->fnode = fk;
	g->mapping = map_newMap(n, fk, vertexMap);
	map_addAll(g->mapping, &nodes, &(void*)value);
	return g;
}




int graph_addEdge(Graph* g, void* v, void* u){
	int* i = malloc(sizeof(int));
	*i = *(int*)map_getValue(g->mapping, v);
	int* j = malloc(sizeof(int));
	*j = *(int*)map_getValue(g->mapping, u);
	if(i < g->n && j < g->n){
		list_addLast(g->edges[*i],j);
		list_addLast(g->edges[*j],i);
	}
	else return 1;
	return 0;
}

int graph_deleteEdge(Graph* g, void* v, void* u){
	int i = *(int*)map_getValue(g->mapping, v);
	int* j = malloc(sizeof(int));
	*j = *(int*)map_getValue(g->mapping, u);
	if(i < g->n && j < g->n) {
		list_remove(g->edges[i],j);
		list_remove(g->edges[j],i);
	}
	else return 1;
	return 0;
}

int lookup(void* v, void* u){
	return *(int*)v == *(int*)u;
}

int graph_hasEdge(Graph* g, void* v, void* u){
	int i = *(int*)map_getValue(g->mapping, v);
	int* j = malloc(sizeof(int));
	*j = *(int*)map_getValue(g->mapping, u);
	if(i < g->n && j < g->n){
		return list_search(g->edges[i],j);
	}
	else return 1;
}

